package flexulous.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

import javax.swing.*;

import org.gjt.sp.jedit.AbstractOptionPane;
import org.gjt.sp.jedit.Buffer;
import org.gjt.sp.jedit.GUIUtilities;
import org.gjt.sp.jedit.MiscUtilities;
import org.gjt.sp.jedit.Mode;
import org.gjt.sp.jedit.jEdit;
import org.gjt.sp.jedit.buffer.FoldHandler;
import org.gjt.sp.jedit.buffer.JEditBuffer;
import org.gjt.sp.util.StandardUtilities;
import org.gjt.sp.util.Log;

import flexulous.*;
import flexulous.model.*;
import flexulous.util.*;
import org.gjt.sp.jedit.browser.VFSBrowser;
import java.awt.*;

public class FlexulousTargetEditorPane extends AbstractOptionPane
{
	
	private FlexulousTarget target;
	
	private JTextField sourceField;
	private JTextField outputField;
	private JTextField configField;
	private JTextField nameField;
	private JCheckBox debugBox;
	
	

	public FlexulousTargetEditorPane(FlexulousTarget target)
	{
		super("Target Editor");
		this.target = target;
		init();
	}

	//{{{ _init() method
	@Override
	protected void _init()
	{
		addComponent(GUIUtilities.createMultilineLabel(jEdit.getProperty("editor.flexulous.caption")));

		addSeparator("editor.flexulous.properties");
		
		nameField = addTextField(target.getName(), jEdit.getProperty("editor.flexulous.name"));
		
		sourceField = addFileSelector(target.getSource(), jEdit.getProperty("editor.flexulous.choose"), jEdit.getProperty("editor.flexulous.source"));
		outputField = addFileSelector(target.getOutput(), jEdit.getProperty("editor.flexulous.choose"), jEdit.getProperty("editor.flexulous.output"));
		
		String configFieldStr = "";
		if(target.getConfigFiles().size() > 0)
		{
			configFieldStr = target.getConfigFiles().get(0);
		}
		configField = addFileSelector(configFieldStr, jEdit.getProperty("editor.flexulous.choose"), jEdit.getProperty("editor.flexulous.config"));
		
		debugBox = addCheckBox(target.getDebug(), jEdit.getProperty("editor.flexulous.debug"));

	} //}}}

	//{{{ _save() method
	@Override
	protected void _save()
	{
		boolean dirtyCompile = false;
		
		if(!target.getName().equals(nameField.getText()))
		{
			target.setName(nameField.getText());
		}
		
		if(!(target.getSource().equals(sourceField.getText())))
		{
			target.setSource(sourceField.getText());
			dirtyCompile = true;
		}
		if(!(target.getOutput().equals(outputField.getText())))
		{
			target.setOutput(outputField.getText());
			dirtyCompile = true;
		}
		if( target.getDebug() != debugBox.isSelected())
		{
			target.setDebug(debugBox.isSelected());
			dirtyCompile = true;
		}
		
		String configFile = configField.getText();
		ArrayList configs = target.getConfigFiles();
			
		if(target.getConfigFiles().size() > 0){
			//We have some existing config files.
			String targetConfig = (String)configs.get(0);
			if(configFile.equals("")){
				//We had a config, we don't anymore.
				configs.remove(0);
				
			}else if(!(configFile.equals(targetConfig))){
					//set us up the config file.
					configs.set(0, configFile);
			}else{
				//value has not changed.
			}
			
		}else{
			//We have no config files, add it if the field isn't blank.
			if(!(configFile.equals(""))){
				configs.add(configFile);
			}
		}
		// Write to the model.
		FlexulousPlugin.getInstance().getModel().write();
		
		//If certain values have been changed, we'll need to recompile.
		if(dirtyCompile){
			target.notifyTargetDirty();
		}
		
		target.notifyTargetChanged();
		
	} //}}}
	
	
	public boolean validateTarget()
	{
		String name = nameField.getText();
		if(name.equals("")){
			GUIUtilities.error(null, "editor.flexulous.nameRequired", null);
			return false;
		}
		String source = sourceField.getText();
		if(source.equals("")){
			GUIUtilities.error(null, "editor.flexulous.sourceRequired", null);
			return false;
		}	
		String output = outputField.getText();
		if(output.equals("")){
			GUIUtilities.error(null, "editor.flexulous.outputRequired", null);
			return false;
		}
		
		return true;
	}
	
	
	private JCheckBox addCheckBox(boolean selected, String textFieldLabel)
	{
		JCheckBox box = new JCheckBox();
		box.setSelected(selected);
		addComponent(textFieldLabel, box);
		return box;
		
	}
	
	private JTextField addTextField(String defaultTextFieldValue, String textFieldLabel)
	{
		JTextField field = new JTextField(defaultTextFieldValue);
		addComponent(textFieldLabel, field);
		return field;
	}
	
	private JTextField addFileSelector(String defaultTextFieldValue, String buttonLabel, String textFieldProperty)
	{
		JTextField field = new JTextField(defaultTextFieldValue);
		JButton button = new JButton(buttonLabel);
		button.addActionListener(new VFSActionHandler(field));

		JPanel panel = new JPanel(new BorderLayout(0, 0));
		panel.add(field, BorderLayout.CENTER);
		panel.add(button, BorderLayout.EAST);

		addComponent(textFieldProperty, panel);

		return field;
	}
	

	//{{{ ActionHandler() class
	private class ActionHandler implements ActionListener
	{
		//{{{ actionPerformed() method
		public void actionPerformed(ActionEvent evt)
		{
			Object source = evt.getSource();
		} //}}}
	} //}}}
	
	private class VFSActionHandler implements ActionListener
	{
		private JTextField field;
		
		public VFSActionHandler(JTextField field){
			this.field = field;
		}
		
		// begin ActionListener implementation
		public void actionPerformed(ActionEvent evt) {
			String[] paths = GUIUtilities.showVFSFileDialog(jEdit.getActiveView(), null,
					 VFSBrowser.OPEN_DIALOG, false);
			if (paths != null) {
				field.setText(paths[0]);
			}
		}
	}
	

}
